package com.yinhe.music.yhmusic.music.list;

import com.apkfuns.logutils.LogUtils;
import com.yinhe.music.common.utils.JsonUtil;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.IBaseModel;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.db.DataBaseAccessor;
import com.yinhe.music.yhmusic.db.bean.MenuDBEntity;
import com.yinhe.music.yhmusic.db.bean.SongDBEntity;
import com.yinhe.music.yhmusic.model.Music;
import com.yinhe.music.yhmusic.network.response.ResponseTransformer;
import com.yinhe.music.yhmusic.utils.SongUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;

/**
 * {@link MusicListFragment} P层
 *
 * @author 何晓华
 */
public class MusicListPresenter extends BasePresenter<IBaseModel, IMusicListContract.IMusicListView> implements IMusicListContract.BaseMusicListPresenter {
    @Override
    public void getMusicList(int page, int pageSize, int id, String type) {
        IMusicListContract.IMusicListView mView = getView();

        if (mView == null) {
            return;
        }

        if (page == 1) {
            mView.showLoading();
        }

        if (type.equals(IConstants.SONGMENU)) {
            addSubscription(mModel.getMusicList(type, id, null, null)
                    .compose(ResponseTransformer.handleResult())
                    .compose(schedulerProvider.applySchedulers())
                    .subscribe(music -> {
                        parseMenuMusicList(music);
                        mView.hideLoading();
                    }, throwable -> {
                        mView.hideLoading();
                        mView.setLoadMoreFailUI();
                        mView.showMessage(throwable);
                    }));
        } else {
            addSubscription(mModel.getMusicList(type, id, String.valueOf(page), String.valueOf(pageSize))
                    .compose(ResponseTransformer.handleResult())
                    .compose(schedulerProvider.applySchedulers())
                    .subscribe(music -> {
                        mView.hideLoading();
                        mView.setMusicListUI(music);
                    }, throwable -> {
                        mView.hideLoading();
                        mView.setLoadMoreFailUI();
                        mView.showMessage(throwable);
                    }));
        }
    }

    private void parseMenuMusicList(Music music) {
        String songListStr = music.getSongListStr();
        List<Music> musicList = JsonUtil.parseNoHeaderJArray(songListStr, Music.class);
        getView().setSongMenuMusicListUI(musicList);
    }

    @Override
    public List<Music> initLocalSongList(int menuId) {
        MenuDBEntity menuDBEntity = DataBaseAccessor.getInstance().getMenuByIdSyn(menuId);

        if (menuDBEntity == null) {
            return null;
        }

        String songs = menuDBEntity.getSongIds();

        List<SongDBEntity> mList = DataBaseAccessor.getInstance().getSongListByIdArray(SongUtil.getSongIdList(songs));
        if (mList == null) {
            return null;
        }

        List<Music> musicList = new ArrayList<>();
        for (SongDBEntity songDBEntity : mList) {
            if (songDBEntity.isSyn()) {
                Music music = new Music(songDBEntity);
                LogUtils.d(music);
                musicList.add(music);
            }
        }

        return musicList;
    }

    @Override
    public void getSearchList(int page, int pageSize, String keyword) {
        if (getView() == null) {
            return;
        }

        if (page == 1) {
            getView().showLoading();
        }

        Disposable disposable = mModel.getSearchList(page, pageSize, keyword, IConstants.SONG)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(searchInfo -> {
                            getView().hideLoading();
                            getView().setMusicListUI(new Music(searchInfo.getSongList(), searchInfo.getPageNum()));
                        },
                        throwable -> {
                            getView().hideLoading();
                            getView().showMessage(throwable);
                        });

        mDisposable.add(disposable);
    }

    @Override
    public void getBoughtList() {
        if (getView() == null) {
            return;
        }

        getView().showLoading();

        Disposable disposable = mModel.getBoughtMusicList()
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(music -> {
                            getView().hideLoading();
                            getView().setMusicListUI(music);
                        },
                        throwable -> {
                            getView().hideLoading();
                            getView().showMessage(throwable);
                        });

        mDisposable.add(disposable);
    }

    @Override
    public void getNewList() {
        if (getView() == null) {
            return;
        }

        getView().showLoading();

        addSubscription(mModel.getNewSongList()
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(music -> {
                            getView().hideLoading();
                            getView().setMusicListUI(music);
                        },
                        throwable -> {
                            getView().hideLoading();
                            getView().showMessage(throwable);
                        }));
    }
}
